//
//  String+Extension.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/10/12.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

extension String {
    
    /// 获取首字母（大写）
    var firstLetter:String {
        
        //转变成可变字符串
        let mutableString = NSMutableString.init(string: self)
        
        //将中文转换成带声调的拼音
        CFStringTransform(mutableString as CFMutableString, nil,      kCFStringTransformToLatin, false)
        
        //去掉声调
        let pinyinString = mutableString.folding(options:          String.CompareOptions.diacriticInsensitive, locale:   NSLocale.current)
        
        //将拼音首字母换成大写
        let strPinYin = polyphoneStringHandle(nameString: self, pinyinString: pinyinString).uppercased()
        
        //截取大写首字母
        let firstString = String(strPinYin[..<strPinYin.index(strPinYin.startIndex, offsetBy: 1)])
        
        //判断首字母是否为大写
        let regexA = "^[A-Z]$"
        let predA = NSPredicate.init(format: "SELF MATCHES %@", regexA)
        return predA.evaluate(with: firstString) ? String(firstString) : "#"
    }
    
    //多音字处理，根据需要添自行加
    private func polyphoneStringHandle(nameString: String, pinyinString: String) -> String {
        if nameString.hasPrefix("长") {return "chang"}
        if nameString.hasPrefix("沈") {return "shen"}
        if nameString.hasPrefix("厦") {return "xia"}
        if nameString.hasPrefix("地") {return "di"}
        if nameString.hasPrefix("重") {return "chong"}
        return pinyinString
    }
    
    //    ----------文字宽高------------------
    
    func widthForComment(fontSize: CGFloat, height: CGFloat = 15) -> CGFloat {
        
        return NSString(string: self).widthForComment(fontSize:fontSize,height:height)
    }
    
    func heightForComment(fontSize: CGFloat, width: CGFloat) -> CGFloat {

        return NSString(string: self).heightForComment(fontSize:fontSize,width:width)
    }
    
    func heightForComment(fontSize: CGFloat, width: CGFloat, maxHeight: CGFloat) -> CGFloat {
        
        return NSString(string: self).heightForComment(fontSize:fontSize, width:width, maxHeight:maxHeight)
    }
}

extension NSString {
    
    
    @objc func widthForComment(fontSize: CGFloat, height: CGFloat = 15) -> CGFloat {
        
        let font = UIFont.systemFont(ofSize: fontSize)
        let rect = self.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil)
        return ceil(rect.width)
    }
    
    @objc func heightForComment(fontSize: CGFloat, width: CGFloat) -> CGFloat {
        
        let font = UIFont.systemFont(ofSize: fontSize)
        let rect = self.boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil)
        return ceil(rect.height)
    }
    
    @objc func heightForComment(fontSize: CGFloat, width: CGFloat, maxHeight: CGFloat) -> CGFloat {
        
        let font = UIFont.systemFont(ofSize: fontSize)
        let rect = self.boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil)
        return ceil(rect.height)>maxHeight ? maxHeight : ceil(rect.height)
    }
}

